//
//  BFShZhiFViewController.m
//  BeautyFood
//
//  Created by apple on 2020/4/29.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THZhiFuViewController.h"

@interface THZhiFuViewController ()
@property(nonatomic, strong) STLabel                     *timeLable;/**<  */
@property(nonatomic, weak) NSTimer                     *timer;/**<  */
@property(nonatomic, assign) NSInteger                     time_count;/**<  */
@property(nonatomic, assign) NSInteger                     pay_index;/**<  */
@property(nonatomic, assign) NSInteger                     selcted_index;/**<  */
@end

@implementation THZhiFuViewController
- (void)dealloc{
    NSLog(@"dealloc");
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支付方式";
    self.selcted_index = 0;
    [self stoneobs_ui_configSubView];
    [self stoneobs_ui_config_table_footer];

    // Do any additional setup after loading the view.
}

#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    __weak typeof(self) weakSelf =  self;
    header.backgroundColor = UIColor.whiteColor;
    CGFloat top = 24;
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, top, STONEOBS_UI_UISCREEN_WIDTH, 30)
                                                                text:@"支付金额"
                                                           textColor:STONEOBS_UI_SECEND_TEXT_COLOR
                                                                font:12
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentCenter];
                [header addSubview:stoneobs_ui_label];
        top = stoneobs_ui_label.bottom;
        self.timeLable = stoneobs_ui_label;
    });
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, top, STONEOBS_UI_UISCREEN_WIDTH, 60)
                                                                text:[NSString stringWithFormat:@"￥%0.2f",self.pay_money.floatValue]
                                                           textColor:STMC_UICOLOR_FROM_RGBA(0xFF2F3D)
                                                                font:38
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentCenter];
        [header addSubview:stoneobs_ui_label];
        stoneobs_ui_label.attributedText = [stoneobs_ui_label.text stoneobs_fun_st_convertAttributeStringWithKeyWord:@"￥" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
        top = stoneobs_ui_label.bottom;
    });
    
    
    ({
        UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(0, top, STONEOBS_UI_UISCREEN_WIDTH, 5)];
        stoneobs_ui_line.backgroundColor = self.tableView.backgroundColor;
        [header addSubview:stoneobs_ui_line];
        top = stoneobs_ui_line.bottom;
    });
    
    ({
        UIView * view = [TMUtils stoneobs_fun_titleButtonViewWithTile:@"支付方式" showRightGo:NO handle:^{
            
        }];
        view.top = top;
        top=view.bottom;
        [header addSubview:view];
    });
    header.height = top;
    self.tableView.tableHeaderView = header;
    


    
}
- (void)stoneobs_ui_config_table_footer{

    UIView * footer = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    
    NSInteger i = 0;
    CGFloat top = 10;
    __weak typeof(self) weakSelf =  self;

        ({
            BOOL selcted = NO;
            if (self.selcted_index == i) {
                selcted = YES;
            }
            STButton * button = [self button_form_left_title:@"支付宝" imageName:@"zhifubao" selcted:selcted];
            button.top = top;
            top = button.bottom + 10;
            [footer addSubview:button];
            if (selcted) {
                [button stoneobs_fun_st_showRoundLayer];
            }
            [button setClicAction:^(UIButton *sender) {
                weakSelf.selcted_index = i;
                [weakSelf stoneobs_ui_config_table_footer];
            }];
            i ++;
        });
        
        ({
            BOOL selcted = NO;
            if (self.selcted_index == i) {
                selcted = YES;
            }
            STButton * button = [self button_form_left_title:@"微信" imageName:@"weixin" selcted:selcted];
            button.top = top;
            top = button.bottom + 10;
            [footer addSubview:button];
            if (selcted) {
                [button stoneobs_fun_st_showRoundLayer];
            }
            [button setClicAction:^(UIButton *sender) {
                weakSelf.selcted_index = i;
                [weakSelf stoneobs_ui_config_table_footer];
            }];
            
        });
 
    
    
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(20, top + 20, STONEOBS_UI_UISCREEN_WIDTH - 40, 44)
                                                  title:@"支付"
                                             titleColor:UIColor.whiteColor
                                              titleFont:18
                                           cornerRadius:22
                                        backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                        backgroundImage:nil
                                                  image:nil];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    button.centerX = self.tableView.width / 2;
    [footer addSubview:button];
    [button setClicAction:^(UIButton *sender) {
        [weakSelf zhifu_order_request];
    }];
    footer.height = button.bottom + 10;

    self.tableView.tableFooterView = footer;
    
    
    
}
- (STButton*)button_form_left_title:(NSString*)leftTitle imageName:(NSString*)imageName selcted:(BOOL)selcted{
    STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(15, 0, STONEOBS_UI_UISCREEN_WIDTH-30, 64)
                                                              title:leftTitle
                                                         titleColor:WH_UICOLOR_FROM_RGBA(0x2C2C2C)
                                                          titleFont:14
                                                       cornerRadius:8
                                                    backgroundColor:UIColor.whiteColor
                                                    backgroundImage:nil
                                                              image:ST_IMAGE_NAMED(imageName)];
    stoneobs_ui_button.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    stoneobs_ui_button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    
    UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    stoneobs_ui_imageView.image = [UIImage imageNamed:@"wxzzf"];
    [stoneobs_ui_button addSubview:stoneobs_ui_imageView];
    stoneobs_ui_imageView.right = stoneobs_ui_button.width - 15;
    stoneobs_ui_imageView.centerY = stoneobs_ui_button.height / 2;
    if (selcted) {
        stoneobs_ui_imageView.image = [UIImage imageNamed:@"xzzhifu"];
    }
    return stoneobs_ui_button;
    
}

#pragma mark --stoneobs Action Method


- (void)zhifu_order_request{

    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    [stoneobs_net_param_dic setObject:@"order" forKey:@"type"];

    if (self.selcted_index == 0 ) {
        [stoneobs_net_param_dic setObject:@"alipay" forKey:@"payType"];
    }else{
        [stoneobs_net_param_dic setObject:@"wechat" forKey:@"payType"];
    }

    if (self.order_type.length) {
        [stoneobs_net_param_dic setObject:self.order_type forKey:@"type"];
        [stoneobs_net_param_dic setObject:self.order_id forKey:@"id"];
    }else{
        if (self.order_id.length) {
            [stoneobs_net_param_dic setObject:self.order_id forKey:@"ids"];
        }
    }
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/pay/getSign"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        if (self.selcted_index == 0) {
            NSString * stoneobs_net_model_string = [responseObject valueForKey:@"data"];
            [TMAliPayManger.manger startPayOrder:stoneobs_net_model_string handle:^(BOOL success, NSString *alert) {
                if (success) {
                    [SVProgressHUD showSuccessWithStatus:@"支付成功"];
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }else{
                    [SVProgressHUD showErrorWithStatus:alert];
                }
            }];
        }else{
            NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
            [TMWechatSocietyManger.manger startPayOrderDic:stoneobs_net_model_dic payHandle:^(BOOL success, NSString *alert) {
                if (success) {
                    [SVProgressHUD showSuccessWithStatus:@"支付成功"];
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }else{
                    [SVProgressHUD showErrorWithStatus:alert];
                }
            }];
        }
        
        
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
@end
